package cn.hicard.risk.action.bd;

import java.io.ByteArrayOutputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import cn.hicard.core.Page;
import cn.hicard.core.PageResult;
import cn.hicard.core.Where;
import cn.hicard.risk.common.Constant;
import cn.hicard.risk.entity.BdTradefileinfo;
import cn.hicard.risk.io.PosTradeFileAnalyzer;
import cn.hicard.risk.report.ReportTemplet;
import cn.hicard.risk.service.bd.TradefileinfoService;
import cn.hicard.risk.vo.Poscollectwarning;
import cn.hicard.risk.vo.PoscollectwarningVo;
import cn.hicard.risk.vo.PoscreditoftentimeVo;
import cn.hicard.risk.vo.Poscreditratio;
import cn.hicard.risk.vo.PoscreditratioVo;
import cn.hicard.risk.vo.Postradedeviant;

/**
 * 用于FLEX端调用，传统POS的商户交易报表查询和下载
 * @author zhangyu
 *
 */
@Controller
@RequestMapping("/ctpos")
public class PostradeAction {
	
	private static Logger logger = Logger.getLogger(Constant.LOGGER_MAIN);
	
	@Autowired
	private TradefileinfoService tradefileinfoService;
	
	
	/**
	 * 分页查询导入的传统POS交易数据
	 * @param trade条件
	 * @param page分页
	 * @return
	 * @throws Exception
	 */
	@RequestMapping
	public String search(
			@RequestParam(required=false,defaultValue="1") int index, 
            @RequestParam(required=false,defaultValue="20") int size,
            @RequestParam(required=false) String wpmerchno,
            @RequestParam(required=false) String merchno,
            @RequestParam(required=false) String termno,
            @RequestParam(required=false) String merchname,
            @RequestParam(required=false) String importdatetime,
            @RequestParam(required=false) String transDateStart,
            @RequestParam(required=false) String transDateEnd,
            ModelMap map
           )throws Exception{
		Where where = new Where(BdTradefileinfo.class);
		where.addEqual("wpmerchno", wpmerchno);
		where.addEqual("merchno", merchno);
		where.addEqual("termno", termno);
		where.addLike("merchname", merchname);
		where.addEqual("importdatetime", importdatetime);
		where.addBetween("transdate", transDateStart, transDateEnd);
        Page page = new Page(index - 1, size);
        PageResult<BdTradefileinfo> result = tradefileinfoService.search(where, page);
        map.addAttribute("list", result.getDataList());
        map.addAttribute("totalcount", result.getResultCount());
        
        return "/ctpos/list";
	}
	
	/**
	 * 读入文件并分析内容，然后返回分析的结果
	 * @throws Exception 
	 * */
	@RequestMapping("/upload")
	public List<BdTradefileinfo> uploadFile(MultipartFile file, String area) throws Exception{
		
		// 分析数据
		List<BdTradefileinfo> list = null;
		PosTradeFileAnalyzer analyzer = new PosTradeFileAnalyzer();
		try{
			list = analyzer.analyze(file.getInputStream(), area);
			tradefileinfoService.saveList(list);
		} catch (Exception e) {
			logger.error("分析交易文件的数据时出错", e);
			throw e;
		}	
		
		return list;
	}
	
	
	private Object[] splitStr(String s){
		if(s != null) {
			String[] strs = s.trim().split(","); // 半角空格
			Object[] objArr = new Object[strs.length];
			for (int i = 0; i < objArr.length; i++) {
				objArr[i] = strs[i].trim();
			}
			return objArr;
		}else{
			return null;
		}
	}
		
	/** 查询:2.1.传统pos商户的交易报表
	 * 注：按条件筛选就是的
	 * @throws Exception */ 
	public String findPostradeinfo(String startDate, String endDate, 
			String merchName, String merchNo, String wpmerchNo,
			ModelMap map) throws Exception {
		Where where = new Where(BdTradefileinfo.class);
		where.addBetween("transdate", startDate, endDate);
		where.addLike("merchname", merchName);		
		where.addIn("merchno", splitStr(merchNo));
		where.addIn("wpmerchno", splitStr(wpmerchNo));
		// 只查消费类型的交易
		where.addEqual("transtype", Constant.TRADE_TYPE_CONSUME);
		List<BdTradefileinfo> list = tradefileinfoService.findByWhere(where);
		map.addAttribute("list", list);
		return "pos/pos_trade";
	}
	
	
	/** 下载:2.1.传统pos商户的交易报表 
	 * @throws Exception */
	public byte[] downloadPostradeinfo(String startDate, String endDate, 
			String merchName, String merchNo, String wpmerchNo) throws Exception {	
		Where where = new Where(BdTradefileinfo.class);
		where.addBetween("transdate", startDate, endDate);
		where.addLike("merchname", merchName);		
		where.addIn("merchno", splitStr(merchNo));
		where.addIn("wpmerchno", splitStr(wpmerchNo));
		// 只查消费类型的交易
		where.addEqual("transtype", Constant.TRADE_TYPE_CONSUME);
		List<BdTradefileinfo> list = tradefileinfoService.findByWhere(where);
		
		// 报表模板
		ReportTemplet rt = new ReportTemplet();
		String title = "传统pos商户交易报表";
		ByteArrayOutputStream os = rt.createPostradeTemplet(list, title, startDate, endDate);
		
		return os.toByteArray();
	}

	
	/** 查询: 2.2.传统pos商户信用卡比例报表
	 * 注：按商户和终端分组，查询信用卡的笔数和金额各自所占比例 
	 * @throws Exception */
	public PoscreditratioVo findPoscreditratio(String startDate, String endDate,
			String merchName, String merchNo, String wpmerchNo) throws Exception {

		Where where = new Where(BdTradefileinfo.class);
		where.addBetween("transdate", startDate, endDate);
		where.addLike("merchname", merchName);
		where.addIn("merchno", splitStr(merchNo));
		where.addIn("wpmerchno", splitStr(wpmerchNo));
		// 只查消费类型的交易
		where.addEqual("transtype", Constant.TRADE_TYPE_CONSUME);
		try {
			return tradefileinfoService.findPoscreditratio(where);
		} catch (Exception e) {
			logger.error("查询: 2.2.传统pos商户信用卡比例报表 发生异常" , e);
			throw e;
		}
	}
	
	/** 下载: 2.2.传统pos商户信用卡比例报表 
	 * @throws Exception */
	public byte[] downloadPoscreditratio(String startDate, String endDate,
			String merchName, String merchNo, String wpmerchNo) throws Exception {
		// 报表模板
		ReportTemplet rt = new ReportTemplet();		
		// 模板的数据源
		PoscreditratioVo creditratio = null;
		try {
			creditratio = findPoscreditratio(startDate, endDate, merchName, merchNo, wpmerchNo);
		} catch (Exception e) {
			logger.error("下载: 2.2.传统pos商户信用卡比例报表 发生异常" , e);
			throw e;
		}
		
		String title = "传统pos商户信用卡比例报表";
		ByteArrayOutputStream os = rt.createCreditratioTemplet(creditratio, title, startDate, endDate);
		
		return os.toByteArray();
	}
	
	/** 查询:2.3.传统pos商户非营业时间交易报表 
	 * 注：查询晚上23点至早上7之间的交易
	 * @throws Exception */
	public List<BdTradefileinfo> findPosnotbusetime(String startDate, String endDate,
			String merchName, String merchNo, String wpmerchNo) throws Exception {

		Where where = new Where(BdTradefileinfo.class);
		where.addBetween("transdate", startDate, endDate);
		where.addLike("merchname", merchName);
		where.addIn("merchno", splitStr(merchNo));
		where.addIn("wpmerchno", splitStr(wpmerchNo));
		// 只查消费类型的交易
		where.addEqual("transtype", Constant.TRADE_TYPE_CONSUME);
		// 非营业时间，由于用到函数，所以暂时不检字段
		where.setMode(Where.MODE_NOT_CHECK);
		// 时间大于23点或小于7点的
		where.startBracket();
		where.addGreaterEq("SUBSTRING(transtime, 5, 6)", "230000");
		where.setMode(Where.MODE_JOINER_OR);
		where.addLessEq("SUBSTRING(transtime, 5, 6)", "070000");
		where.endBracket();
		// 不正确，集合为空：where.addBetween("SUBSTRING(transtime, 5, 6)", "230000", "070000");
		// 设置回来，不然会影响查询模式
		where.setMode(Where.MODE_CHECK);
		try {
			return tradefileinfoService.findByWhere(where);
		} catch (Exception e) {
			logger.error("查询:2.3.传统pos商户非营业时间交易报表 发生异常" , e);
			throw e;
		}
	}
	/** 下载:2.3.传统pos商户非营业时间交易报表 
	 * @throws Exception */
	public byte[] downloadPosnotbusetime(String startDate, String endDate,
			String merchName, String merchNo, String wpmerchNo) throws Exception {
		
		// 报表模板
		ReportTemplet rt = new ReportTemplet();
		
		// 模板的数据源
		List<BdTradefileinfo> list = null;

		try {
			list = findPosnotbusetime(startDate, endDate, merchName, merchNo, wpmerchNo);
		} catch (Exception e) {
			logger.error("下载:2.3.传统pos商户非营业时间交易报表 发生异常" , e);
			throw e;
		}
		
		String title = "传统pos商户非营业时间交易报表";
		ByteArrayOutputStream os = rt.createPostradeTemplet(list, title, startDate, endDate);
		
		return os.toByteArray();		
	}

	
	/** 查询：2.4.传统pos商户大额交易报表 
	 * 注：同终端、同卡号、同一天累计>=20万之后的交易
	 * @throws Exception */
	public List<BdTradefileinfo> findPosriskctrl(String startDate, String endDate,
			String merchName, String merchNo, String wpmerchNo) throws Exception {

		Where where = new Where(BdTradefileinfo.class);
		where.addBetween("transdate", startDate, endDate);
		where.addLike("merchname", merchName);
		where.addIn("merchno", splitStr(merchNo));
		where.addIn("wpmerchno", splitStr(wpmerchNo));
		// 只查消费类型的交易
		where.addEqual("transtype", Constant.TRADE_TYPE_CONSUME);
		// 剩下的条件运算交给业务层去作，这是普通SQL办不到的
		try {
			return tradefileinfoService.findPosriskctrl(where);
		} catch (Exception e) {
			logger.error("查询：2.4.传统pos商户大额交易报表 发生异常" , e);
			throw e;
		}
	}
	
	/** 下载:2.4.传统pos商户大额交易报表 
	 * @throws Exception */
	public byte[] downloadPosriskctrl(String startDate, String endDate,
			String merchName, String merchNo, String wpmerchNo) throws Exception {
		
		// 报表模板
		ReportTemplet rt = new ReportTemplet();
		
		// 模板的数据源
		List<BdTradefileinfo> list = null;
		
		try {
			list = findPosriskctrl(startDate, endDate, merchName, merchNo, wpmerchNo);
		} catch (Exception e) {
			logger.error("下载:2.4.传统pos商户大额交易报表  发生异常" , e);
			throw e;
		}
		
		String title = "传统pos商户大额交易报表（≥20万）";
		ByteArrayOutputStream os = rt.createPostradeTemplet(list, title, startDate, endDate);
		
		return os.toByteArray();
	}

	/** 查询: 2.5.传统pos商户信用卡频繁交易报表 
	 * 注1：同一个终端、同一张信用卡、同一天、连续刷2笔>=3000元
	 * 注2：同一个终端、同一张信用卡、同一天、连续刷4笔>=10元
	 * @throws Exception */
	public PoscreditoftentimeVo findPoscreditoftentime(String startDate, String endDate, String merchName, String merchNo,String wpmerchNo) throws Exception {
		try {
			PoscreditoftentimeVo vo = new PoscreditoftentimeVo();

			Where where = new Where(BdTradefileinfo.class);
			where.addBetween("transdate", startDate, endDate);
			where.addLike("merchname", merchName);
			where.addIn("merchno", splitStr(merchNo));
			where.addIn("wpmerchno", splitStr(wpmerchNo));
			// 只查消费类型的交易
			where.addEqual("transtype", Constant.TRADE_TYPE_CONSUME);
			// 只查信用卡类型
			where.addEqual("cardtype", "1");
			
			vo.creditoftentime_2_3000 = tradefileinfoService.findPoscreditoftentime(where, 3000.00, 2);
			vo.creditoftentime_4_10 = tradefileinfoService.findPoscreditoftentime(where, 10.00, 4);
			return vo;
		} catch (Exception e) {
			logger.error("查询: 2.5.传统pos商户信用卡频繁交易报表  发生异常" , e);
			throw e;
		}
	}

	/** 下载: 2.5.传统pos商户信用卡频繁交易报表 
	 * @throws Exception */
	public byte[] downloadPoscreditoftentime(String startDate, String endDate,
			String merchName, String merchNo, String wpmerchNo) throws Exception {
		// 报表模板
		ReportTemplet rt = new ReportTemplet();
		
		// 模板的数据源
		PoscreditoftentimeVo vo = null;
		try {
			vo = findPoscreditoftentime(startDate, endDate, merchName, merchNo, wpmerchNo);
		} catch (Exception e) {
			logger.error("下载: 2.5.传统pos商户信用卡频繁交易报表   发生异常" , e);
			throw e;
		}
		
		String title = "传统pos商户信用卡频繁交易报表";
		ByteArrayOutputStream os = rt.createPoscreditoftentimeTemplet(vo, title, startDate, endDate);
		
		return os.toByteArray();
	}
	
	/** 查询: 2.6.传统pos商户信用卡大额交易报表 
	 *  注：信用卡单笔交易>=1万的交易
	 * @throws Exception */
	public List<BdTradefileinfo> findPoscreditriskctrl(String startDate, String endDate,
			String merchName, String merchNo, String wpmerchNo) throws Exception {

		Where where = new Where(BdTradefileinfo.class);
		where.addBetween("transdate", startDate, endDate);
		where.addLike("merchname", merchName);
		where.addIn("merchno", splitStr(merchNo));
		where.addIn("wpmerchno", splitStr(wpmerchNo));
		// 只查消费类型的交易
		where.addEqual("transtype", Constant.TRADE_TYPE_CONSUME);
		// 信用卡类型
		where.addEqual("cardtype", "1");
		// 交易额>=1w
		where.setMode(Where.MODE_NOT_CHECK);
		where.addGreaterEq("(transmoney+0.00)", 10000.00);
		where.setMode(Where.MODE_CHECK);
		try {
			return tradefileinfoService.findByWhere(where);
		} catch (Exception e) {
			logger.error("查询: 2.6.传统pos商户信用卡大额交易报表   发生异常" , e);
			throw e;
		}
	}
	
	/** 下载: 2.6.传统pos商户信用卡大额交易报表 
	 * @throws Exception */
	public byte[] downloadPoscreditriskctrl(String startDate, String endDate,
			String merchName, String merchNo, String wpmerchNo) throws Exception {
		// 报表模板
		ReportTemplet rt = new ReportTemplet();
		
		// 模板的数据源
		List<BdTradefileinfo> list = null;

		try {
			list = findPoscreditriskctrl(startDate, endDate, merchName, merchNo, wpmerchNo);
		} catch (Exception e) {
			logger.error("下载: 2.6.传统pos商户信用卡大额交易报表    发生异常" , e);
			throw e;
		}
		
		String title = "传统pos商户信用卡大额交易报表";
		ByteArrayOutputStream os = rt.createPostradeTemplet(list, title, startDate, endDate);
		
		return os.toByteArray();
	}

	/** 查询: 2.7.传统pos商户交易偏离值报表 
	 * 注：比较两段时期内的交易比例
	 * @throws Exception */
	public List<Postradedeviant> findPostradedeviant(
			String startDate1, String endDate1,
			String startDate2, String endDate2, double change,
			String merchName, String merchNo, String wpmerchNo) throws Exception {

		Where where1 = new Where(BdTradefileinfo.class);
		where1.addBetween("transdate", startDate1, endDate1);
		where1.addLike("merchname", merchName);
		where1.addIn("merchno", splitStr(merchNo));
		where1.addIn("wpmerchno", splitStr(wpmerchNo));
		// 只查消费类型的交易
		where1.addEqual("transtype", Constant.TRADE_TYPE_CONSUME);
		
		Where where2 = new Where(BdTradefileinfo.class);
		where2.addBetween("transdate", startDate2, endDate2);
		where2.addLike("merchname", merchName);
		where2.addIn("merchno", splitStr(merchNo));
		where2.addIn("wpmerchno", splitStr(wpmerchNo));
		// 只查消费类型的交易
		where2.addEqual("transtype", Constant.TRADE_TYPE_CONSUME);
		try {
			return tradefileinfoService.findPostradedeviant(where1, where2, change);
		} catch (Exception e) {
			logger.error("查询: 2.7.传统pos商户交易偏离值报表    发生异常" , e);
			throw e;
		}
	}
	
	/** 下载: 2.7.传统pos商户交易偏离值报表 
	 * @throws Exception */
	public byte[] downloadPostradedeviant(String startDate1, String endDate1,
			String startDate2, String endDate2, double change,
			String merchName, String merchNo, String wpmerchNo) throws Exception {
		// 报表模板
		ReportTemplet rt = new ReportTemplet();
		
		// 模板的数据源
		List<Postradedeviant> list = null;

		
		try {
			list = findPostradedeviant(startDate1, endDate1, startDate2, endDate2, change, merchName, merchNo, wpmerchNo);
		} catch (Exception e) {
			logger.error("下载: 2.7.传统pos商户交易偏离值报表  发生异常" , e);
			throw e;
		}
		
		String title = "传统pos商户交易偏离值报表 ";
		ByteArrayOutputStream os = rt.createPostradedeviant(list, title, startDate1, endDate1, startDate2, endDate2, change);
		
		return os.toByteArray();
	}

	/** 查询: 2.8.传统pos商户交易预警汇总报表 
	 * @throws Exception */
	public PoscollectwarningVo findPosriskcollect(String startDate, String endDate,
			String merchName, String merchNo, String wpmerchNo) throws Exception {
	
		try {
			// 用于最终返回的结果
			List<Poscollectwarning> list = new ArrayList<Poscollectwarning>();
			// 用于标记已存的便于进行累加统计
			Map<String, Poscollectwarning> map = new HashMap<String, Poscollectwarning>();
			DecimalFormat df = new DecimalFormat("0.00");
			// 合计数据
			PoscollectwarningVo vo = new PoscollectwarningVo(); 
			//[start] 合计数据 START
			double totalNotbusetimeMoney = 0.00;
			int totalNotbusetimeCount = 0;
			
			double totalPosriskctrlMoney = 0.00;
			int totalPosriskctrlCount = 0;
			
			double totalPoscreditoftentimeMoney3000 = 0.00;
			int totalPoscreditoftentimeCount3000 = 0;
			
			double totalPoscreditriskctrlMoney = 0.00;
			int totalPoscreditriskctrlCount = 0;
			
			double totalPoscreditoftentimeMoney10 = 0.00;
			int totalPoscreditoftentimeCount10 = 0;
			
			double totalPoscreditratioMoney = 0.00;
			int totalPoscreditratioCount = 0;		
			//double totalPoscreditratio = 0;
			//[end] 合计数据 END
			
			
			// 注意：※※※※※※      Poscollectwarning的各数量和金额必须赋初值，否则转换时报空异常
			//非营业时间	
			List<BdTradefileinfo> posnotbusetime = findPosnotbusetime(startDate, endDate, merchName, merchNo, wpmerchNo);
			for (BdTradefileinfo tdf : posnotbusetime) {
				String key = tdf.getMerchno() + "," + tdf.getTermno();
				if(map.containsKey(key)){
					Poscollectwarning pcw = map.get(key);
					double money = Double.parseDouble(tdf.getTransmoney()) + Double.parseDouble(pcw.getNotbusetimeMoney());
					pcw.setNotbusetimeMoney(df.format(money));
					pcw.setNotbusetimeCount(String.valueOf(Integer.parseInt(pcw.getNotbusetimeCount())+ 1));
					map.put(key, pcw);
				}else{
					Poscollectwarning pcw = new Poscollectwarning();
					pcw.setMerchno(tdf.getMerchno());
					pcw.setMerchname(tdf.getMerchname());
					pcw.setTermno(tdf.getTermno());
					pcw.setArea(String.valueOf(tdf.getArea()));
					pcw.setNotbusetimeMoney(df.format(Double.parseDouble(tdf.getTransmoney())));
					pcw.setNotbusetimeCount("1");
					map.put(key, pcw);				
				}	
				totalNotbusetimeMoney += Double.parseDouble(tdf.getTransmoney());
				totalNotbusetimeCount += 1;
			}
			
			//大额交易
			List<BdTradefileinfo> posriskctrl = findPosriskctrl(startDate, endDate, merchName, merchNo, wpmerchNo);
			for (BdTradefileinfo tdf : posriskctrl) {
				String key = tdf.getMerchno() + "," + tdf.getTermno();
				if(map.containsKey(key)){
					Poscollectwarning pcw = map.get(key);
					double money = Double.parseDouble(tdf.getTransmoney()) + Double.parseDouble(pcw.getPosriskctrlMoney());
					pcw.setPosriskctrlMoney(df.format(money));
					pcw.setPosriskctrlCount(String.valueOf(Integer.parseInt(pcw.getPosriskctrlCount())+ 1));
					map.put(key, pcw);
				}else{
					Poscollectwarning pcw = new Poscollectwarning();
					pcw.setMerchno(tdf.getMerchno());
					pcw.setMerchname(tdf.getMerchname());
					pcw.setTermno(tdf.getTermno());
					pcw.setArea(String.valueOf(tdf.getArea()));
					pcw.setPosriskctrlMoney(df.format(Double.parseDouble(tdf.getTransmoney())));
					pcw.setPosriskctrlCount("1");
					map.put(key, pcw);
				}
				totalPosriskctrlMoney += Double.parseDouble(tdf.getTransmoney());
				totalPosriskctrlCount += 1;
			}
			
			//信用卡频繁交易总
			PoscreditoftentimeVo poscreditoftentime = findPoscreditoftentime(startDate, endDate, merchName, merchNo, wpmerchNo);			
			//信用卡频繁交易（2笔3000元以上）	
			List<BdTradefileinfo> poscreditoftentime3000 = poscreditoftentime.creditoftentime_2_3000;
			for (BdTradefileinfo tdf : poscreditoftentime3000) {
				String key = tdf.getMerchno() + "," + tdf.getTermno();
				if(map.containsKey(key)){
					Poscollectwarning pcw = map.get(key);
					double money = Double.parseDouble(tdf.getTransmoney()) + Double.parseDouble(pcw.getPoscreditoftentimeMoney3000());
					pcw.setPoscreditoftentimeMoney3000(df.format(money));
					pcw.setPoscreditoftentimeCount3000(String.valueOf(Integer.parseInt(pcw.getPoscreditoftentimeCount3000())+ 1));
					map.put(key, pcw);
				}else{
					Poscollectwarning pcw = new Poscollectwarning();
					pcw.setMerchno(tdf.getMerchno());
					pcw.setMerchname(tdf.getMerchname());
					pcw.setTermno(tdf.getTermno());
					pcw.setArea(String.valueOf(tdf.getArea()));
					pcw.setPoscreditoftentimeMoney3000(df.format(Double.parseDouble(tdf.getTransmoney())));
					pcw.setPoscreditoftentimeCount3000("1");
					map.put(key, pcw);
				}
				totalPoscreditoftentimeMoney3000 += Double.parseDouble(tdf.getTransmoney());
				totalPoscreditoftentimeCount3000 += 1;
			}
			//信用卡频繁交易（4笔10元以上）	
			List<BdTradefileinfo> poscreditoftentime10 = poscreditoftentime.creditoftentime_4_10;
			for (BdTradefileinfo tdf : poscreditoftentime10) {
				String key = tdf.getMerchno() + "," + tdf.getTermno();
				if(map.containsKey(key)){
					Poscollectwarning pcw = map.get(key);
					double money = Double.parseDouble(tdf.getTransmoney()) + Double.parseDouble(pcw.getPoscreditoftentimeMoney10());
					pcw.setPoscreditoftentimeMoney10(df.format(money));
					pcw.setPoscreditoftentimeCount10(String.valueOf(Integer.parseInt(pcw.getPoscreditoftentimeCount10())+ 1));
					map.put(key, pcw);
				}else{
					Poscollectwarning pcw = new Poscollectwarning();
					pcw.setMerchno(tdf.getMerchno());
					pcw.setMerchname(tdf.getMerchname());
					pcw.setTermno(tdf.getTermno());
					pcw.setArea(String.valueOf(tdf.getArea()));
					pcw.setPoscreditoftentimeMoney10(df.format(Double.parseDouble(tdf.getTransmoney())));
					pcw.setPoscreditoftentimeCount10("1");
					map.put(key, pcw);
				}
				totalPoscreditoftentimeMoney10 += Double.parseDouble(tdf.getTransmoney());
				totalPoscreditoftentimeCount10 += 1;
			}
									
			//信用卡大额交易（1万以上）
			List<BdTradefileinfo> poscreditriskctrl = findPoscreditriskctrl(startDate, endDate, merchName, merchNo, wpmerchNo);		
			for (BdTradefileinfo tdf : poscreditriskctrl) {
				String key = tdf.getMerchno() + "," + tdf.getTermno();
				if(map.containsKey(key)){
					Poscollectwarning pcw = map.get(key);
					double money = Double.parseDouble(tdf.getTransmoney()) + Double.parseDouble(pcw.getPoscreditriskctrlMoney());
					pcw.setPoscreditriskctrlMoney(df.format(money));
					pcw.setPoscreditriskctrlCount(String.valueOf(Integer.parseInt(pcw.getPoscreditriskctrlCount())+ 1));
					map.put(key, pcw);
				}else{
					Poscollectwarning pcw = new Poscollectwarning();
					pcw.setMerchno(tdf.getMerchno());
					pcw.setMerchname(tdf.getMerchname());
					pcw.setTermno(tdf.getTermno());
					pcw.setArea(String.valueOf(tdf.getArea()));
					pcw.setPoscreditriskctrlMoney(df.format(Double.parseDouble(tdf.getTransmoney())));
					pcw.setPoscreditriskctrlCount("1");
					map.put(key, pcw);
				}
				totalPoscreditriskctrlMoney += Double.parseDouble(tdf.getTransmoney());
				totalPoscreditriskctrlCount += 1;
			}

			// 信用卡比例交易		
			// 信用卡的交易数据已经是经过汇总了的，虽然没必要区分了，但还是保持一致
			PoscreditratioVo poscreditratioVo = findPoscreditratio(startDate, endDate, merchName, merchNo, wpmerchNo);
			List<Poscreditratio> poscreditratios = poscreditratioVo.poscreditratios;
			for (Poscreditratio pct : poscreditratios) {
				String key = pct.getMerchno() + "," + pct.getTermno();
				if(map.containsKey(key)){
					Poscollectwarning pcw = map.get(key);
					pcw.setPoscreditratioMoney(df.format(Double.parseDouble(pct.getCredittransmoney())));
					pcw.setPoscreditratioCount(pct.getCredittranscount());
					//占总交易额比例(已经是经过汇总的了，无需重新累加计算)
					pcw.setPoscreditratio(pct.getMoneyratio());
					map.put(key, pcw);
				}else{
					Poscollectwarning pcw = new Poscollectwarning();
					pcw.setMerchno(pct.getMerchno());
					pcw.setMerchname(pct.getMerchname());
					pcw.setTermno(pct.getTermno());
					pcw.setArea(String.valueOf(pct.getArea()));
					pcw.setPoscreditratioMoney(df.format(Double.parseDouble(pct.getCredittransmoney())));
					pcw.setPoscreditratioCount(pct.getCredittranscount());
					//占总交易额比例(已经是经过汇总的了，无需重新累加计算)
					pcw.setPoscreditratio(pct.getMoneyratio());
					map.put(key, pcw);				
				}
				totalPoscreditratioMoney += Double.parseDouble(pct.getCredittransmoney());
				totalPoscreditratioCount += Integer.parseInt(pct.getCredittranscount());			
			}
			
			list.addAll(map.values());
			
			vo.poscollectwarnings = list;
			// 组装合计数据
			vo.totalNotbusetimeMoney = df.format(totalNotbusetimeMoney);
			vo.totalNotbusetimeCount = String.valueOf(totalNotbusetimeCount);
			
			vo.totalPosriskctrlMoney = df.format(totalPosriskctrlMoney);
			vo.totalPosriskctrlCount = String.valueOf(totalPosriskctrlCount);
			
			vo.totalPoscreditoftentimeMoney3000 = df.format(totalPoscreditoftentimeMoney3000);
			vo.totalPoscreditoftentimeCount3000 = String.valueOf(totalPoscreditoftentimeCount3000);
			
			vo.totalPoscreditriskctrlMoney = df.format(totalPoscreditriskctrlMoney);
			vo.totalPoscreditriskctrlCount = String.valueOf(totalPoscreditriskctrlCount);
			
			vo.totalPoscreditoftentimeMoney10 = df.format(totalPoscreditoftentimeMoney10);
			vo.totalPoscreditoftentimeCount10 = String.valueOf(totalPoscreditoftentimeCount10);
			
			vo.totalPoscreditratioMoney = df.format(totalPoscreditratioMoney);
			vo.totalPoscreditratioCount = String.valueOf(totalPoscreditratioCount);
			// 总金额需要遍历统计，不过FLEX端本身要重新遍历，于是就交给FLEX运算了
			//double totalPoscreditratio = 0; 
			
			return vo;
		} catch (Exception e) {
			logger.error("查询: 2.8.传统pos商户交易预警汇总报表  发生异常" , e);
			throw e;
		}
	}
	
	/** 下载: 2.8.传统pos商户交易预警汇总报表 
	 * @throws Exception */
	public byte[] downloadPosriskcollect(String startDate, String endDate,
			String merchName, String merchNo, String wpmerchNo) throws Exception {
		// 报表模板
		ReportTemplet rt = new ReportTemplet();
		
		// 模板的数据源
		PoscollectwarningVo vo = null;
		
		try {
			vo = findPosriskcollect(startDate, endDate, merchName, merchNo, wpmerchNo);
		} catch (Exception e) {
			logger.error("下载: 2.8.传统pos商户交易预警汇总报表  发生异常" , e);
			throw e;
		}
		
		String title = "传统pos商户交易预警汇总报表 ";
		ByteArrayOutputStream os = rt.createPoscollectwarningTemplet(vo, title, startDate, endDate);
		
		return os.toByteArray();
	}

}
